package cn.itsource.org.service.impl;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class Departmentimpl extends BaseServiceImpl<Department,DepartmentQuery> implements IDepartmentService {
    @Autowired
    private RedisService redisService;
//    注入mapper
    @Autowired
    private DepartmentMapper mapper;
    @Override
    public PageList<Department> findPage(DepartmentQuery query) {
//        PageList<Department> redisPageList = redisService.getKeyObjectValue("departments", PageList.class);
//        if (redisPageList != null){
//            return redisPageList;
//        }

        //1.分页插件的使用
        PageHelper.startPage(query.getPage(),query.getSize());

        //2.执行分页语句 1，2顺序不能反  ，中间不能写代码
        List<Department> departments = mapper.findPage(query);

        //3.封装到pageInfo里面去
        PageInfo<Department> pageInfo = new PageInfo<>(departments);

        //4.封装自定义pageList对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
//        将pageList对象存入redis
//        redisService.setStringKeyAndValue("departments",pageList,60);
        return pageList;
    }

    @Override
    public void update(Department department) {
        //1.获取到级联的数组值
        Long[] parentIds = department.getManagerIds();
        if (parentIds != null){
            if (parentIds.length-1<0 ){
//                throw new RuntimeException("抱歉,暂时无法执行此操作!");
                department.setParentId(null);
            }else {
                //设置上级id
                department.setParentId(parentIds[parentIds.length-1]);
            }
            StringBuilder sb = new StringBuilder("");
            for (Long parentId : parentIds) {
                if (parentId==department.getId()){
                   throw new RuntimeException("禁止添加该级联关系!");
                }else {
                    sb.append("/").append(parentId);
                }
            }
            sb.append("/").append(department.getId());
            department.setDirPath(sb.toString());
        }else{
            department.setDirPath("/"+department.getId());
        }
        //只有设置了值的字段才会被修改
        mapper.updateByPrimaryKey(department);
    }
    @Override
    public void save(Department department) {
        mapper.insertSelective(department);
        this.update(department);
//        只有设置了值的字段才会被添加

    }
    @Override
    public List<Department> deptTree() {
        //1.查询所有的部门数据，作为递归的基础数据
        List<Department> departments = mapper.deptTree();
        //2.通过递归工具类，解析成无限极数据
        List<Department> tree = TreeUtil.getTree(departments);
        return tree;
    }
}
